package com.google.code.sip.ucenter.service;

import java.util.List;

import com.google.code.sip.core.dao.Page;
import com.google.code.sip.ucenter.model.Credit;
import com.google.code.sip.ucenter.model.CreditLog;
import com.google.code.sip.ucenter.model.CreditOperation;
import com.google.code.sip.ucenter.model.CreditRule;
import com.google.code.sip.ucenter.model.CreditRuleLog;
import com.google.code.sip.ucenter.model.CreditType;
import com.google.code.sip.ucenter.model.User;
import com.google.code.sip.ucenter.model.UserCount;

public interface CreditManager {

	Credit getCredit(String creditId);

	Credit getCredit(User user, CreditType creditType);

	Credit getOrCreateCredit(User user, CreditType creditType);

	Page<Credit> getAllCredites(Page<Credit> page);

	List<Credit> getAllCredites();

	List<Credit> getCreditesByUser(User user);

	List<Credit> getCreditesByUsers(List<User> users);

	void saveCredit(Credit entity);

	void deleteCredit(String creditId);

	void grantCredit(User user, String action, String remark);

	List<CreditLog> getCreditLogs(User user);

	Page<CreditLog> getCreditLogs(User user, Page<CreditLog> page);

	CreditType getCreditTypeByName(String name);

	List<CreditType> getAllCreditTypes();

	CreditRule getCreditRuleByAction(String action);

	List<CreditRule> getAllCreditRules();

	void transferCredit(Credit outCredit, Credit intCredit, int amount,
			String remark);

	List<CreditRuleLog> getAllCreditRuleLog(User user);

	CreditRuleLog getCreditRuleLog(User user, CreditRule creditRule);

	/**
	 * 更新用户总积分
	 * 
	 * @param user
	 */
	void updateUserCreditCount(User user);

	/**
	 * 计算用户总积分
	 * 
	 * @param user
	 * @return
	 */
	int calcUserCreditCount(User user);

	/**
	 * 判断是否可以计算积分
	 * 
	 * @param creditRule
	 * @param creditRuleLog
	 * @return
	 */
	boolean canGrantCredit(CreditRule creditRule, CreditRuleLog creditRuleLog);

	void chargeCredit(User user, double money, CreditType creditType,
			int creditChargeRatio, String remark);

	// -------------------------------------------------------------- the
	// following add by fengcai 2010/7/9

	/**
	 * 积分消费
	 * 
	 * @param credit
	 * @param creditOperation
	 * @param amount
	 * @param remark
	 * @return
	 */
	String buy(Credit credit, CreditOperation creditOperation, int amount,
			String remark);

	/**
	 * 取消积分消费
	 * 
	 * @param user
	 *            用户
	 * @param creditType
	 *            积分类型
	 * @param transactionId
	 *            积分交易日志ID
	 * @param deductedTax
	 *            是否扣税
	 */
	void cancelBuy(User user, CreditType creditType, String transactionId,
			boolean deductedTax, CreditOperation creditOperation);

	/**
	 * 积分收益
	 * 
	 * @param user
	 *            用户
	 * @param creditType
	 *            积分类别
	 * @param creditOperation
	 *            积分操作类型
	 * @param deductedTax
	 *            是否扣税
	 * @param amount
	 *            积分值
	 * @param remark
	 */
	void gainCredit(User user, CreditType creditType,
			CreditOperation creditOperation, boolean deductedTax, int amount,
			String remark);

	void sell(User user, CreditType creditType, int amount, String remark);

	/**
	 * 兑换积分（从一种积分类型兑换成另外 一种积分类型）
	 * 
	 * @param user
	 * @param creditTypeFrom
	 * @param creditTypeTo
	 * @param amount
	 * @param remark
	 */
	void exchange(User user, CreditType creditTypeFrom,
			CreditType creditTypeTo, int amount, String remark);

	/**
	 * 重新计算所有用户积分
	 */
	void reCalcUserCreditCount();

	UserCount getOrCreateUserCreditCount(User user);

}
